package com.example.demo.runner;

import com.example.demo.properties.ProjectProps;
import com.example.demo.properties.project.ProjectModel;
import com.example.demo.util.JacksonUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 软件著作权:王艳明
 * 系统名称:SpringBoot后台管理系统
 *
 * @author WangYanming
 * @ClassName 类:ProjectRunner.java
 * @Description 功能描述:project
 * @Version 1.0
 * @date 2025-10-11 下午 02:31
 */
@Component
@Order(value = 1)
@Slf4j
public class ProjectRunner implements ApplicationRunner {

    @Autowired
    private ProjectProps projectProps;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("yml.name:{}", projectProps.getName());
        log.info("yml.description:{}", projectProps.getDescription());
        log.info("yml.summary:{}", projectProps.getSummary());
        List<String> fruits = projectProps.getFruits();
        if (!CollectionUtils.isEmpty(fruits)) {
            int i = 0;
            for (String fruit : fruits) {
                i++;
                log.info("yml.fruits." + i + ":{}", fruit);
            }
        }
        List<String> colors = projectProps.getColors();
        if (!CollectionUtils.isEmpty(colors)) {
            int i = 0;
            for (String color : colors) {
                i++;
                log.info("yml.colors." + i + ":{}", color);
            }
        }
        log.info("yml.project.name:{}", projectProps.getProjectName());
        Map<String, Object> projectMap = projectProps.getProject();
        // 当formatFlag为true时，序列化后的 JSON 会有缩进和格式化，便于人类阅读
        log.info("yml.project原:{}", JacksonUtil.objectToJson(projectMap, true));
        /*如下方式也可以,适合结构正常,次数yml结构不正常,应该为list=[obj1,obj2],结果却为map={"0":obj1,"1":obj2}
            ObjectMapper objectMapper = new ObjectMapper();
            ProjectModel projectModel = objectMapper.convertValue(projectMap, ProjectModel.class);
        */
        ProjectModel projectModel = ProjectModel.mapToObj(projectMap);
        log.info("yml.project新:{}", JacksonUtil.objectToJson(projectModel, true));

        log.info("yml.server.ssl.certificate:{}", projectProps.getCertificate());
    }
}
